package com.aiti.lulian.service.impl;

import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardVersionUpdatesDto;
import com.aiti.lulian.entity.CardVersionUpdates;
import com.aiti.lulian.mapper.CardVersionUpdatesMapper;
import com.aiti.lulian.service.ICardVersionUpdatesService;
import com.aiti.lulian.vo.CardVersionUpdatesVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CardVersionUpdatesServiceImpl extends BaseSerivceImpl<CardVersionUpdatesMapper, CardVersionUpdates> implements ICardVersionUpdatesService {

    @Override
    public Message releaseCardVersionUpdates(CardVersionUpdatesDto dto) {
        String version = dto.getVersion();
        List<CardVersionUpdates> list = this.list(new LambdaQueryWrapper<CardVersionUpdates>().eq(CardVersionUpdates::getVersion, version));
        if (! list.isEmpty()) {
            return Message.fail("版本号重复");
        }
        CardVersionUpdates updates = new CardVersionUpdates();
        BeanUtils.copyProperties(dto, updates);
        updates.setCreator(dto.getUserId());
        updates.setBaseCreateTime(new Date());
        this.save(updates);
        return Message.success();
    }

    @Override
    public List<CardVersionUpdatesVo> getCardVersionUpdatesList(CardVersionUpdatesDto dto, Page<CardVersionUpdates> page) {
        return this.baseMapper.selectCardVersionUpdatesList(dto, page);
    }

    @Override
    public CardVersionUpdatesVo getCardVersionUpdatesDetail(String baseId) {
        return this.baseMapper.selectCardVersionUpdatesDetail(baseId);
    }

    @Override
    public CardVersionUpdatesVo getCardVersionUpdatesLatest() {
        return this.baseMapper.selectCardVersionUpdatesLatest();
    }

    @Override
    public void deleteCardVersionUpdatesBatch(CardVersionUpdatesDto dto) {
        List<String> updatesIds = dto.getUpdatesIds();
        this.removeByIds(updatesIds);
    }
}
